package moe.oil.db.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import moe.oil.dto.user.UserTargetDTO;
import moe.oil.entity.BaseEntity;
import moe.oil.enumeration.ExamType;

import javax.persistence.*;
import java.time.LocalDate;

@Table(name = "user_target")
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class UserTarget implements BaseEntity<UserTargetDTO> {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_target_id_seq")
    private Long targetId;

    @OneToOne(mappedBy = "userTarget", cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
    private User user;

    @Column(name = "exam_type")
    private ExamType examType;

    @Column(name = "location")
    private String location;

    @Column(name = "target_major")
    private String targetMajor; //todo 专业可以设置多个，或者用逗号分隔，或者用enum

    @Column(name = "examYear")
    private LocalDate examYear;

    public UserTarget(UserTargetDTO userTargetDTO) {
        this.examType = userTargetDTO.getExamType();
        this.location = userTargetDTO.getLocation();
        this.targetMajor = userTargetDTO.getTargetMajor();
        this.examYear = userTargetDTO.getExamYear();
    }

    @Override
    public String toString() {
        return String.format("UserTarget{id=%s, examType=%s, location=%s, targetMajor=%s, examYear=%s}",
                targetId, examType, location, targetMajor, examYear);
    }

    @Override
    public UserTargetDTO convert() {
        return new UserTargetDTO()
                .setTargetMajor(targetMajor)
                .setExamType(examType)
                .setExamYear(examYear)
                .setLocation(location);
    }
}
